//
//  TCTopicViewController.m
//  Project_B
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "TCTopicViewController.h"
#import "AFNetworkHandler.h"
#import "TCdetailRoute.h"
#import "TCContextTableViewCell.h"
#import "TCHeaderTableViewCell.h"
#import "MBProgressHUD.h"
#import "UIImage+GIF.h"
//#define WIDTH [[UIScreen mainScreen] bounds].size.width / 375
//#define HEIGHT [[UIScreen mainScreen] bounds].size.height / 667
@interface TCTopicViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSDictionary *dic;
@property (nonatomic, strong)UILabel *namelabel;
@property (nonatomic, strong)NSMutableArray *tempArr;
@property (nonatomic, strong)MBProgressHUD *hud;

@end

@implementation TCTopicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.dic = [NSDictionary dictionary];
    self.tempArr = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self creattableView];
    [self getTopData];
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:NO];
    self.hud.mode = MBProgressHUDModeIndeterminate;

}
- (void)creattableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375*WIDTH, (667-64)*HEIGHT) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[TCContextTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[TCHeaderTableViewCell class] forCellReuseIdentifier:@"headcell"];

}
- (void)getTopData
{
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v3/club/topics/%@/",self.temp.topic_id];
//    @"http://api.lis99.com/v3/club/topics/1004872/";
    [AFNetworkHandler requestPOSTWithURL:str body:nil comletion:^(id result) {
//        NSLog(@"%@",result);
        self.dic = result;
        [self.tableView reloadData];
        [self.hud removeFromSuperview];
    }];
    NSString *datastr = [NSString stringWithFormat:@"http://api.lis99.com/v2/club/replylist/%@/?page=0?page=0&topic_id=%@",self.temp.topic_id,self.temp.topic_id];
    [AFNetworkHandler getUrl:datastr cookie:nil result:^(id result) {
        self.tempArr = [TCdetailRoute transformWithArray:result[@"data"][@"topiclist"]];
        [self.tableView reloadData];
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tempArr.count+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        TCHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"headcell"];
        cell.dic = self.dic;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        TCContextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.temp = self.tempArr[indexPath.row-1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        CGFloat h = [TCHeaderTableViewCell heightWithText:self.dic[@"data"][@"content"]];
        return h+180*HEIGHT;
    } else {
        CGFloat h1 = [TCContextTableViewCell heightWithText:[self.tempArr[indexPath.row-1] content]];
        return h1+350*HEIGHT;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
